package com.zhike.b2b.data;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhike.b2b.base.BaseService;

public class DataMgr extends BaseService
{
	 Logger log = LoggerFactory.getLogger(DataMgr.class);
	 
  private String _username = "";
  private String _password = "";
  private String _database = "";
  private String _host = "";
  private String _port = "";

  public DataMgr(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    this._username = paramString1;
    this._password = paramString2;
    this._database = paramString3;
    this._host = paramString4;
    this._port = paramString5;
  }

  /*public ArrayList getTables()
  {
    ArrayList localArrayList = jt.qu("show table");
    return localArrayList;
  }*/

  public boolean toBak(String paramString)
  {
    try
    {
      Runtime localRuntime = Runtime.getRuntime();
      Process localProcess = localRuntime.exec(getShellString(paramString));
      int i = localProcess.waitFor();
      return i == 0;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return false;
  }

  public String getShellString(String paramString)
  {
    String str1 = paramString;
    try
    {
      File localFile = new File(paramString + "/data/");
      if (!localFile.exists())
      {
        localFile.mkdir();
        log.debug("新建目录成功");
      }
    }
    catch (Exception localException)
    {
      log.debug("新建目录出错");
      localException.printStackTrace();
    }
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    StringBuffer localStringBuffer = new StringBuffer();
    String str2 = System.getProperty("os.name");
    String str3 = "";
    if (str2.indexOf("Windows") != -1)
      str3 = paramString + "/include/tool/mysqldumpwin.exe -h " + this._host + " -P" + this._port + "   -u" + this._username + " -p" + this._password + " --result-file=" + paramString + "/data/" + localSimpleDateFormat.format(new Date()) + ".sql   --default-character-set=gbk " + this._database;
    else
      str3 = paramString + "/include/tool/mysqldump -h " + this._host + " -P" + this._port + "   -u" + this._username + " -p" + this._password + " --result-file=" + paramString + "/data/" + localSimpleDateFormat.format(new Date()) + ".sql  " + this._database;
    return str3;
  }

  public static ArrayList getBakInfo(String paramString)
  {
    String str = paramString + "/data/";
    File localFile1 = new File(str);
    if (!localFile1.exists())
      return null;
    if (!localFile1.isDirectory())
      return null;
    String[] arrayOfString = localFile1.list();
    Arrays.sort(arrayOfString);
    ArrayList localArrayList = new ArrayList();
    for (int i = arrayOfString.length - 1; i >= 0; i--)
    {
      File localFile2 = new File(str + arrayOfString[i]);
      if (!localFile2.isFile())
        continue;
      DataFile localDataFile = new DataFile();
      localDataFile.setFileName(arrayOfString[i]);
      localDataFile.setFileSize(localFile2.length());
      localDataFile.setSavePath(localFile2.getPath());
      localArrayList.add(localDataFile);
    }
    return localArrayList;
  }

  public boolean toCommbak(String paramString1, String paramString2)
  {
    try
    {
      Runtime localRuntime = Runtime.getRuntime();
      String str = getCommbakShell(paramString1, paramString2);
      String[] arrayOfString;
      Process localProcess;
      if (System.getProperty("os.name").indexOf("Windows") != -1)
      {
        arrayOfString = new String[] { "cmd", "/c", str };
        localProcess = localRuntime.exec(arrayOfString);
      }
      else
      {
        arrayOfString = new String[] { "sh", "-c", str };
        localProcess = localRuntime.exec(arrayOfString);
      }
      log.debug(str);
      int i = localProcess.waitFor();
      log.debug("进程返回值为tag:" + i);
      return i == 0;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return false;
  }

  public String getCommbakShell(String paramString1, String paramString2)
  {
    String str1 = paramString1;
    String str2 = System.getProperty("os.name");
    String str3 = "";
    if (str2.indexOf("Windows") != -1)
      str3 = paramString1 + "/include/tool/mysqlwin.exe -h " + this._host + " -P" + this._port + "   -u" + this._username + " -p" + this._password + " --default-character-set=gbk " + this._database + " < " + paramString1 + "/data/" + paramString2 + ".sql";
    else
      str3 = paramString1 + "/include/tool/mysql -h " + this._host + " -P" + this._port + "   -u" + this._username + " -p" + this._password + "   --default-character-set=gbk " + this._database + " < " + paramString1 + "/data/" + paramString2 + ".sql";
    return str3;
  }

  public static void main(String[] paramArrayOfString)
  {
    DataMgr localDataMgr = new DataMgr("b2b_v442_dev", "111111", "b2b_v442_dev", "192.168.10.115", "3306");
    //log.debug(localDataMgr.getCommbakShell("d:/", "test.sql"));
  }

  public String get_database()
  {
    return this._database;
  }

  public void set_database(String paramString)
  {
    this._database = paramString;
  }

  public String get_password()
  {
    return this._password;
  }

  public void set_password(String paramString)
  {
    this._password = paramString;
  }

  public String get_username()
  {
    return this._username;
  }

  public void set_username(String paramString)
  {
    this._username = paramString;
  }
}

